import ctypes
import locale
import markdown
from enum import Enum

from utils.YamlHelper import YamlHelper

_lang: YamlHelper | None = None

def get_default_lang():
    return locale.windows_locale[ctypes.windll.kernel32.GetUserDefaultUILanguage()]

def init_lang(lang_path, lang=None):
    global _lang
    if lang is None:
        lang = get_default_lang()
    _lang = YamlHelper()
    _lang.read(f'{lang_path}/{lang}.yaml')
    if _lang.is_new_yaml:
        # 初始化为默认语言
        _lang.read(f'{lang_path}/zh_CN.yaml')

def get_lang(key: Enum, get_type=str, is_markdown=False):
    """key是一个Enum，名称以L_开头"""
    key = key.name[2:]
    ret = _lang.get(key, get_type=get_type)
    if is_markdown:
        ret = markdown.markdown(ret)
    return ret

def is_new_lang():
    return _lang.is_new_yaml